﻿Imports System.Data.OleDb

Public Class frmCompra

    Dim coneccion As OleDbConnection

    Private Sub frmCompra_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        actulizaGridCompras()
        actulizaGridProveedor()

        dgvCompra.RowHeadersVisible = False
        dgvCompra.ClearSelection()

        dgvProveedor.RowHeadersVisible = False
        dgvProveedor.ClearSelection()

        dgvCompra.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
        dgvProveedor.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill

        dgvProveedor.Columns(2).Visible = False
        dgvProveedor.Columns(3).Visible = False
        dgvProveedor.Columns(4).Visible = False
        dgvProveedor.Columns(5).Visible = False

        dgvCompra.Columns(5).Visible = False

    End Sub

    Private Sub btnAgregarDetalle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregarDetalle.Click
        frmDetalleCompra.Show()
        frmDetalleCompra.lblID.Text = dgvCompra.Item(0, dgvCompra.CurrentRow.Index).Value
        Me.Hide()
    End Sub

    Sub actulizaGridCompras()
        Dim orden As OleDbDataAdapter
        Dim tabla As DataSet

        coneccion = New OleDbConnection(bd.sConexion)

        orden = New OleDbDataAdapter("select a.IdCompra, a.NumeroFactura, b.Nombre as Proveedor, a.Fecha, a.MontoTotal, a.IdProveedor from encabezado_Compra as a, proveedor as b where a.IdProveedor = b.IdProveedor", coneccion)
        tabla = New DataSet

        orden.Fill(tabla, "Compras")

        dgvCompra.DataSource = tabla

        dgvCompra.DataMember = "Compras"
    End Sub

    Sub actulizaGridProveedor()
        Dim orden As OleDbDataAdapter
        Dim tabla As DataSet

        coneccion = New OleDbConnection(bd.sConexion)

        orden = New OleDbDataAdapter("select * from proveedor", coneccion)
        tabla = New DataSet

        orden.Fill(tabla, "Proveedor")

        dgvProveedor.DataSource = tabla

        dgvProveedor.DataMember = "Proveedor"
    End Sub

    Private Sub dgvCompra_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvCompra.CellClick
        btnAgregarDetalle.Enabled = True

        If IsDBNull(dgvCompra.Item(1, dgvCompra.CurrentRow.Index).Value) Then
            txtNFactura.Text = ""
        Else
            txtNFactura.Text = dgvCompra.Item(1, dgvCompra.CurrentRow.Index).Value
        End If

        If IsDBNull(dgvCompra.Item(2, dgvCompra.CurrentRow.Index).Value) Then
            txtNFactura.Text = ""
        Else
            txtProveedor.Text = dgvCompra.Item(2, dgvCompra.CurrentRow.Index).Value
        End If

        If IsDBNull(dgvCompra.Item(3, dgvCompra.CurrentRow.Index).Value) Then
            txtNFactura.Text = ""
        Else
            dtpFecha.Text = dgvCompra.Item(3, dgvCompra.CurrentRow.Index).Value
        End If

        If IsDBNull(dgvCompra.Item(4, dgvCompra.CurrentRow.Index).Value) Then
            txtNFactura.Text = ""
        Else
            txtMonto.Text = dgvCompra.Item(4, dgvCompra.CurrentRow.Index).Value
        End If

        btnGuardar.Text = "Nuevo"
        btnActualizar.Enabled = True
        btnEliminar.Enabled = True
    End Sub

    Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        If btnGuardar.Text = "Nuevo" Then
            limpiaCampos()
            btnEliminar.Enabled = False
            btnActualizar.Enabled = False
            btnGuardar.Text = "Guardar"
            btnAgregarDetalle.Enabled = False

            txtNFactura.Focus()
        Else
            Dim command As OleDbCommand

            Dim query1 As String = "insert into encabezado_Compra(NumeroFactura, IdProveedor, Fecha, MontoTotal) values('" & txtNFactura.Text & "'," & dgvProveedor.Item(0, dgvProveedor.CurrentRow.Index).Value & ",'" & dtpFecha.Text & "'," & txtMonto.Text & ")"
            command = New OleDbCommand(query1, coneccion)
            command.Connection.Open()
            command.ExecuteNonQuery()
            command.Connection.Close()

            MsgBox("El registro fue Ingresado")

            actulizaGridCompras()
            dgvProveedor.ClearSelection()
            limpiaCampos()
        End If
    End Sub

    Sub limpiaCampos()
        txtNFactura.Clear()
        txtProveedor.Clear()
        dtpFecha.Text = Now
        txtMonto.Clear()
        dgvCompra.ClearSelection()
        dgvProveedor.ClearSelection()
    End Sub

    Private Sub dgvProveedor_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvProveedor.CellClick
        txtProveedor.Text = dgvProveedor.Item(1, dgvProveedor.CurrentRow.Index).Value
    End Sub

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
        Dim command As OleDbCommand

        coneccion = New OleDbConnection(bd.sConexion)

        Dim query1 As String = "delete from encabezado_Compra WHERE IdCompra =" & dgvCompra.Item(0, dgvCompra.CurrentRow.Index).Value
        command = New OleDbCommand(query1, coneccion)
        command.Connection.Open()
        command.ExecuteNonQuery()
        command.Connection.Close()

        MsgBox("El registro fue Eliminado")

        command.Connection.Close()

        actulizaGridCompras()
        dgvProveedor.ClearSelection()
        limpiaCampos()

        btnGuardar.Text = "Guardar"
        btnEliminar.Enabled = False
        btnActualizar.Enabled = False
        btnAgregarDetalle.Enabled = False
    End Sub

    Private Sub btnActualizar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnActualizar.Click
        Dim command As OleDbCommand

        coneccion = New OleDbConnection(bd.sConexion)

        Dim idproveedor

        If (dgvProveedor.CurrentRow.Index = 0) Then
            idproveedor = dgvCompra.Item(5, dgvCompra.CurrentRow.Index).Value
        Else
            idproveedor = dgvProveedor.Item(0, dgvProveedor.CurrentRow.Index).Value
        End If

        Dim query1 As String = "UPDATE encabezado_Compra SET NumeroFactura=" + txtNFactura.Text + ", IdProveedor=" + idproveedor.ToString + ", Fecha='" + dtpFecha.Text + "', MontoTotal=" + txtMonto.Text + " WHERE IdCompra=" & dgvCompra.Item(0, dgvCompra.CurrentRow.Index).Value

        command = New OleDbCommand(query1, coneccion)
        command.Connection.Open()
        command.ExecuteNonQuery()
        command.Connection.Close()
        MsgBox("El registro fue Modificado")

        command.Connection.Close()
        actulizaGridCompras()
        dgvProveedor.ClearSelection()
        limpiaCampos()

        btnGuardar.Text = "Guardar"
        btnEliminar.Enabled = False
        btnActualizar.Enabled = False
        btnAgregarDetalle.Enabled = False
    End Sub
End Class